﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MOJsdk;
using System.Threading;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for StyleInspector.xaml
    /// </summary>
    public partial class StyleInspector : Window
    {
        private ObservableCollection<MyInfo> _calls;
        public StyleInspector()
        {
            InitializeComponent();
            _calls = new ObservableCollection<MyInfo>();
            _listView.ItemsSource = _calls;

            _listView.SelectionChanged += OnSelectionChanged;
            _calls.CollectionChanged += OnCollectionChanged;
        }
        public void Set(IHost HOST)
        {
            HOST.OnStyleCalled += Called;
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1) return;

            _txt_Element.Text = _calls[x].ElementType.FullName;
            _txt_Style.Text = _calls[x].ElementID;
            SetElem(_calls[x]);
        }
        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (_listView.Items.Count < 1) return;

            int last = _listView.Items.Count - 1;
            _listView.ScrollIntoView(_listView.Items[last]);
        }
        private void Called(object sender, ElementInfo e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
            {
                MyInfo inf = new MyInfo()
                {
                    Elem = (FrameworkElement)e.Element,
                    Index = _calls.Count + 1,
                    ElementID = e.ElementID,
                    ElementType = e.ElementType,
                };
                _calls.Add(inf);
            });
        }

        private void SetElem(MyInfo elem)
        {
            SetParents(elem.Elem);
            try { SetContent((ContentControl)elem.Elem); }
            catch { _txtcontent.Text = ""; }
        }
        private void SetParents(FrameworkElement e)
        {
            _listBox.Items.Clear();

            FrameworkElement elem = e;

            while (true)
            {
                try
                {
                    _listBox.Items.Add(elem.GetType().FullName);
                    elem = elem.Parent as FrameworkElement;
                }
                catch
                { return; }
            }
        }
        private void SetContent(ContentControl e)
        {
            _txtcontent.Text = e.Content.ToString();
        }

        public class MyInfo : IEquatable<MyInfo>
        {
            public int Index { get; set; }
            public FrameworkElement Elem { get; set; }
            public string ElementID { get; set; }
            public Type ElementType { get; set; }

            public bool Equals(MyInfo info)
            { return info.Index == this.Index; }
        }
    }
}
